<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2012 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 * @version osc---hanbingshu sanow@126.com
 * @date 2012-07-03
 */
class omeanalysts_mdl_dlyDaily extends dbeav_model{
	
    static private $branch_info;
    function get_schema()
    {
        $schema_obj = kernel::single("omeanalysts_dlyschema");
        $schema = $schema_obj->get_schema();
        foreach($schema['columns'] as $schema_k=>$val)
        {
           $schema['default_in_list'][] = $schema_k;
           $schema['in_list'][] = $schema_k;
        }
        return $schema;
    }

    function getList($cols='*', $filter=array(), $offset=0, $limit=-1, $orderType=null){
        $list = $this->__getList($filter);
        $aData = array_slice((array)$list,$offset,$limit);
        return $aData;
    }
    function __getList($filter=array(),$export=false){
        if($filter['time_from']) $filter['time_from'] = strtotime($filter['time_from']);
        if($filter['time_to']) $filter['time_to'] = strtotime($filter['time_to'])+24*60*60;
        if($filter['type_id']) $filter['branch_id'] = $filter['type_id'];
        $delivery = app::get("ome")->model("delivery");
        if(!$this->branch_info)
        {
            $branch = app::get("ome")->model("branch");
            $branch_data = $branch->getList("branch_id,name");
            $user_branch = kernel::single("ome_userbranch");
            $branch_id_filter = $user_branch->get_user_branch_id();
            foreach($branch_data as $branch_val)
            {
                if(!$branch_id_filter || $export)$this->branch_info[$branch_val['branch_id']] = $branch_val['name'];
                elseif(in_array($branch_val['branch_id'], (array)$branch_id_filter)) $this->branch_info[$branch_val['branch_id']] = $branch_val['name'];
            }
            
        }
        $days = ($filter['time_to']-$filter['time_from'])/(24*60*60);
        if($days>300){echo "时间间隔不能大于300天！";exit;}
        for($i=0;$i<$days;$i++)
        {
            $time_from = $filter['time_from']+$i*24*60*60;
            $time_to = $time_from+24*60*60-1;
            #echo date("Y-m-d H:i:s",$time_from)."到".date("Y-m-d H:i:s",$time_to)."<hr/>";
            $date = date('Y-m-d',$time_from);
            if($filter['branch_id'])
            {
                $aTmp['dly_date'] = $date;
                $where = "and s.branch_id=".$filter['branch_id']." and s.sales_time>=".$time_from." and s.sales_time<=".$time_to;
                $aTmp['branch_id'] = $filter['branch_id'];
                $dely_sql = "select sum(nums) as nums,sum(sales_amount) as amount from sdb_ome_sales_items as s where s.sales_type='0'".$where;
                $dly_data = $delivery->db->selectrow($dely_sql);
                $res_sql = "select sum(nums) as nums,sum(sales_amount) as amount from sdb_ome_sales_items as s where s.sales_type='1'".$where;
                $res_data = $delivery->db->selectrow($res_sql);
                $dly_num_sql = "SELECT count(DISTINCT i.original_id) as dly_nums FROM sdb_ome_sales_items AS s LEFT JOIN sdb_ome_iostock AS i ON s.iostock_id = i.iostock_id where s.sales_type='0'". $where;
                $dly_num_data = $delivery->db->selectrow($dly_num_sql);
                $res_num_sql = "SELECT count(DISTINCT i.original_id) as res_nums FROM sdb_ome_sales_items AS s LEFT JOIN sdb_ome_iostock AS i ON s.iostock_id = i.iostock_id where s.sales_type='1'". $where;
                $res_num_data = $delivery->db->selectrow($res_num_sql);
                $aTmp['delivery_num'] = $dly_num_data['dly_nums'] ?$dly_num_data['dly_nums'] :0;
                $aTmp['reship_num'] = $res_num_data['res_nums']?$res_num_data['res_nums'] : 0;
                $aTmp['amount_p'] = $dly_data['amount']?$dly_data['amount']:0;
                $aTmp['amount_n'] = $res_data['amount']?$res_data['amount'] :0;
                $aTmp['delivery_goods_num'] = $dly_data['nums']?$dly_data['nums']:0;
                $aTmp['reship_goods_num'] = $res_data['nums']?$res_data['nums']:0;
                $aData[] = $aTmp;
            }
            else
            {
                foreach($this->branch_info as $info_k=>$info_v)
                {
                    $aTmp['dly_date'] = $date;
                    $where = "and s.branch_id=".$info_k." and s.sales_time>=".$time_from." and s.sales_time<=".$time_to;
                    $aTmp['branch_id'] = $this->branch_info[$filter['branch_id']];
                    $dely_sql = "select sum(nums) as nums,sum(sales_amount) as amount from sdb_ome_sales_items as s where s.sales_type='0'".$where;
                    $dly_data = $delivery->db->selectrow($dely_sql);
                    $res_sql = "select sum(nums) as nums,sum(sales_amount) as amount from sdb_ome_sales_items as s where s.sales_type='1'".$where;
                    $res_data = $delivery->db->selectrow($res_sql);
                    $aTmp['branch_id'] = $info_k;
                    $dly_num_sql = "SELECT count(DISTINCT i.original_id) as dly_nums FROM sdb_ome_sales_items AS s LEFT JOIN sdb_ome_iostock AS i ON s.iostock_id = i.iostock_id where s.sales_type='0'". $where;
                    $dly_num_data = $delivery->db->selectrow($dly_num_sql);
                    $res_num_sql = "SELECT count(DISTINCT i.original_id) as res_nums FROM sdb_ome_sales_items AS s LEFT JOIN sdb_ome_iostock AS i ON s.iostock_id = i.iostock_id where s.sales_type='1'". $where;
                    $res_num_data = $delivery->db->selectrow($res_num_sql);
                    $aTmp['delivery_num'] = $dly_num_data['dly_nums'] ?$dly_num_data['dly_nums'] :0;
                    $aTmp['reship_num'] = $res_num_data['res_nums']?$res_num_data['res_nums'] : 0;
                    $aTmp['amount_p'] = $dly_data['amount']?$dly_data['amount']:0;
                    $aTmp['amount_n'] = $res_data['amount']?$res_data['amount'] :0;
                    $aTmp['delivery_goods_num'] = $dly_data['nums']?$dly_data['nums']:0;
                    $aTmp['reship_goods_num'] = $res_data['nums']?$res_data['nums']:0;
                    $aData[] = $aTmp;
                }
            }
            
        }
        return $aData;
    }

    function count($filter=null)
    {
        return count($this->__getList($filter));
    }
    public function modifier_branch_id($row){
        if(!$this->branch_info)
        {
            $branch = app::get("ome")->model("branch");
            $branch_data = $branch->getList("branch_id,name");
            foreach($branch_data as $branch_val)
            {
                $this->branch_info[$branch_val['branch_id']] = $branch_val['name'];
            }
            
        }
        return $this->branch_info[$row];
    }

    //报表导出 ====begin
    public function export_params(){       
        //处理filter
        $filter = $this->export_filter;
        if ($post = unserialize($_POST['params'])) {
            $filter['time_from'] = $post['from'];
            $filter['time_to'] = $post['to'];
            $filter['type_id'] = $post['branch_id'];
        }
        $params = array(
            'filter' => $filter,
            'limit' => 1000,
            'get_data_method' => 'get_delivery_data',
            'single'=> array(
                'main'=> array(
                    'filename' => '出货日报表主要信息',
                ),
                'items'=> array(
                    'filename' => '出货日报表明细信息',
                ),
            ),
            'merge' => array(
                array(
                    'mkey' => 'main,items',
                    'filename' => '出货日报表(主体+明细)合并信息',
                ),
            ),
        );
        return $params;
    }

    /**
     * 【定义主体信息title】
     * @return array
     */
     //注：方法名由提供的返回主体信息方法名后加上后缀 _title，系统默认按此规则去找这个方法。
    public function get_delivery_data_title(){
        $title['main'] = array(
            '*:日期',
            '*:仓库',
            '*:发货单数量',
            '*:退货单数量',
            '*:成交金额',
            '*:退款金额',
            '*:发货商品总数量',
            '*:退货商品总数量',
        );
        $title['items'] = array(
            '*:日期',
            '*:仓库',
            '*:货号',
            '*:货品名称',
            '*:发货数量',
            '*:退货数量',
            '*:成交金额',
            '*:退款金额',          
        );
        return $title;
    }
    
    /**
     * 【返回主体信息】
     * @自定义方法
     * @return array
     */
     //注：$filter是在export_params()方法里获取到的filter,$offset,$limit已做处理，直接带到getList里即可
    public function get_delivery_data($filter,$offset,$limit,&$data){
        $aData  = $this->__getList($filter,true);
        $list = array_slice((array)$aData,$offset,$limit);
        foreach($list as $k=>$v){
            $v['branch_name'] = $this->modifier_branch_id($v['branch_id']);
            $data['main'][] = array(
                                '*:日期'=>$v['dly_date'],
                                '*:仓库'=>$v['branch_name'],
                                '*:发货单数量'=>$v['delivery_num'],
                                '*:退货单数量'=>$v['reship_num'],
                                '*:成交金额'=>$v['amount_p'],
                                '*:退款金额'=>$v['amount_n'],
                                '*:发货商品总数量'=>$v['delivery_goods_num'],
                                '*:退货商品总数量'=>$v['reship_goods_num'],
            );
            
            //出货日报明细
            $detail_data = $this->get_day_detail($v['dly_date'],$v['branch_id']);
            if($detail_data)
            {
                foreach((array)$detail_data as $items_k=>$items_v)
                {
                     $data['items'][] = array(
                                        '*:日期'=>$v['dly_date'],
                                        '*:仓库'=>$v['branch_name'],
                                        '*:货号'=>$items_v['dly_bn']?$items_v['dly_bn'] : $items_v['res_bn'],
                                        '*:货品名称'=>$items_v['dly_bn']?$items_v['dly_bn'] : $items_v['res_bn'],
                                        '*:发货数量'=>$items_v['dly_nums'],
                                        '*:退货数量'=>$items_v['res_nums'],
                                        '*:成交金额'=>$items_v['dly_amount'],
                                        '*:退款金额'=>$items_v['res_amount'], 
                    );                     
                }

            }
        } 
    }
    
    //发货单导出 ====end

    /*某一天的发货报表明细*/
    function get_day_detail($date=null,$branch_id=null)
    {
        if(empty($date) || empty($branch_id)) return array();
        $user_branch = kernel::single("ome_userbranch");
        $branch_id_filter= $user_branch->get_user_branch_id();
        if($branch_id_filter && !in_array($branch_id, (array)$branch_id_filter)){echo "仓库不存在或者无该仓库权限;";exit;}
        $from_time = strtotime($date);
        $to_time = strtotime($date)+24*60*60;
        $sales_items = app::get("ome")->model("sales_items");
        $where = " and branch_id=".$branch_id." and sales_time>=".$from_time." and sales_time<=".$to_time;
        $dly_sql = "SELECT sum(nums) as dly_nums,sum(sales_amount) as dly_amount ,bn as dly_bn,name as dly_name from sdb_ome_sales_items where sales_type='0'" .$where." group by bn";
        $dly_data = $sales_items->db->select($dly_sql);
        $res_sql = "SELECT sum(nums) as res_nums,sum(sales_amount) as res_amount ,bn as res_bn,name as res_name from sdb_ome_sales_items where sales_type='1'" .$where." group by bn";
        $res_data = $sales_items->db->select($res_sql);

        $__dly = array();
        foreach((array)$dly_data as $dly_v)
        {
            $__dly[$dly_v['dly_bn']] = $dly_v;
        }
        $__res = array();
        foreach((array)$res_data as $res_v)
        {
            $__res[$res_v['res_bn']] = $res_v;
        }
        return array_merge_recursive ($__dly, $__res);
    }
}
